package org.example.ding.dto;

import jakarta.validation.constraints.*;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
import org.example.ding.entity.User;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 用户个人信息数据传输对象
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class UserProfileDto {

    private Long id;

    private String username;

    @Email(message = "邮箱格式不正确")
    private String email;

    @Size(max = 50, message = "真实姓名长度不能超过50个字符")
    private String realName;

    private User.Gender gender;

    @Min(value = 1, message = "年龄必须大于0")
    @Max(value = 150, message = "年龄不能超过150")
    private Integer age;

    @DecimalMin(value = "50.0", message = "身高必须大于50cm")
    @DecimalMax(value = "300.0", message = "身高不能超过300cm")
    private BigDecimal height;

    @DecimalMin(value = "20.0", message = "体重必须大于20kg")
    @DecimalMax(value = "1000.0", message = "体重不能超过1000kg")
    private BigDecimal weight;

    private User.ActivityLevel activityLevel;

    // 营养目标
    @DecimalMin(value = "800.0", message = "卡路里目标不能少于800")
    @DecimalMax(value = "5000.0", message = "卡路里目标不能超过5000")
    private BigDecimal dailyCaloriesGoal;

    @DecimalMin(value = "30.0", message = "蛋白质目标不能少于30g")
    @DecimalMax(value = "500.0", message = "蛋白质目标不能超过500g")
    private BigDecimal dailyProteinGoal;

    @DecimalMin(value = "50.0", message = "碳水化合物目标不能少于50g")
    @DecimalMax(value = "800.0", message = "碳水化合物目标不能超过800g")
    private BigDecimal dailyCarbsGoal;

    @DecimalMin(value = "20.0", message = "脂肪目标不能少于20g")
    @DecimalMax(value = "200.0", message = "脂肪目标不能超过200g")
    private BigDecimal dailyFatGoal;

    // 饮食限制和过敏信息
    private String dietaryRestrictions;
    private String allergies;

    // 计算字段
    private BigDecimal bmi;
    private BigDecimal bmr;
    private BigDecimal tdee;

    private LocalDateTime createdAt;
    private LocalDateTime updatedAt;

    /**
     * 从User实体创建DTO
     */
    public static UserProfileDto fromUser(User user) {
        UserProfileDto dto = new UserProfileDto();
        dto.setId(user.getId());
        dto.setUsername(user.getUsername());
        dto.setEmail(user.getEmail());
        dto.setRealName(user.getRealName());
        dto.setGender(user.getGender());
        dto.setAge(user.getAge());
        dto.setHeight(user.getHeight());
        dto.setWeight(user.getWeight());
        dto.setActivityLevel(user.getActivityLevel());
        dto.setDailyCaloriesGoal(user.getDailyCaloriesGoal());
        dto.setDailyProteinGoal(user.getDailyProteinGoal());
        dto.setDailyCarbsGoal(user.getDailyCarbsGoal());
        dto.setDailyFatGoal(user.getDailyFatGoal());
        dto.setDietaryRestrictions(user.getDietaryRestrictions());
        dto.setAllergies(user.getAllergies());
        dto.setCreatedAt(user.getCreatedAt());
        dto.setUpdatedAt(user.getUpdatedAt());
        
        // 计算BMI、BMR、TDEE
        dto.setBmi(user.calculateBMI());
        dto.setBmr(user.calculateBMR());
        dto.setTdee(user.calculateTDEE());
        
        return dto;
    }

    /**
     * 更新User实体（不包括用户名和密码）
     */
    public void updateUser(User user) {
        if (email != null) user.setEmail(email);
        if (realName != null) user.setRealName(realName);
        if (gender != null) user.setGender(gender);
        if (age != null) user.setAge(age);
        if (height != null) user.setHeight(height);
        if (weight != null) user.setWeight(weight);
        if (activityLevel != null) user.setActivityLevel(activityLevel);
        if (dailyCaloriesGoal != null) user.setDailyCaloriesGoal(dailyCaloriesGoal);
        if (dailyProteinGoal != null) user.setDailyProteinGoal(dailyProteinGoal);
        if (dailyCarbsGoal != null) user.setDailyCarbsGoal(dailyCarbsGoal);
        if (dailyFatGoal != null) user.setDailyFatGoal(dailyFatGoal);
        if (dietaryRestrictions != null) user.setDietaryRestrictions(dietaryRestrictions);
        if (allergies != null) user.setAllergies(allergies);
    }

    /**
     * 获取BMI状态描述
     */
    public String getBmiStatus() {
        if (bmi == null) {
            return "未知";
        }
        
        double bmiValue = bmi.doubleValue();
        if (bmiValue < 18.5) {
            return "偏瘦";
        } else if (bmiValue < 24) {
            return "正常";
        } else if (bmiValue < 28) {
            return "超重";
        } else {
            return "肥胖";
        }
    }
}
